package com.jlastu.dlxy_androidapp.view.sell;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.jlastu.dlxy_androidapp.R;
import com.jlastu.dlxy_androidapp.adapter.GoodsManageListAdapter;
import com.jlastu.dlxy_androidapp.adapter.StoreGoodsListAdapter;
import com.jlastu.dlxy_androidapp.contract.GoodsManageContract;
import com.jlastu.dlxy_androidapp.entity.Goods;
import com.jlastu.dlxy_androidapp.entity.Photo;
import com.jlastu.dlxy_androidapp.presenter.GoodsManagePresenter;
import com.jlastu.dlxy_androidapp.utils.SpUtils;

import java.util.ArrayList;
import java.util.List;

public class GoodsManageActivity extends AppCompatActivity implements GoodsManageContract.IGoodsManageView {
    private ImageButton mBack;
    private RecyclerView mGoodsRecyclerView;
    private GoodsManageListAdapter mRecyclerAdapter;
    private GoodsManageContract.IGoodsManagePresenter mGoodsManagePresenter;

    private List<Goods> mGoodsList;

    private List<Photo> mPhotoList;
    private SpUtils mSpUtils;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goodsmanage);
        mBack = findViewById(R.id.back_button);
        mGoodsManagePresenter = new GoodsManagePresenter(this, this);
        mGoodsList = new ArrayList<>();
        mPhotoList = new ArrayList<>();
        mGoodsRecyclerView = findViewById(R.id.rv_goods);
        StaggeredGridLayoutManager staggeredGridLayoutManager =
                new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
        mGoodsRecyclerView.setLayoutManager(staggeredGridLayoutManager);
        mRecyclerAdapter = new GoodsManageListAdapter(mGoodsList, mPhotoList,mGoodsManagePresenter);
        mGoodsRecyclerView.setAdapter(mRecyclerAdapter);
        mSpUtils = new SpUtils(getApplicationContext());

        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        //查询当前登录帐号的id
        int user_id = mSpUtils.getInt("userid", 0);
        if (user_id != 0) {
            //根据id查询当前登录用户的信息
            mGoodsManagePresenter.getGoodsList(user_id);
        }
    }

    @Override
    public void showGoodsList(List<Goods> goodsList, List<Photo> photoList) {
        mGoodsList = goodsList;
        mPhotoList = photoList;
        Log.d("", "showGoodsList " + mGoodsList.size());
        mRecyclerAdapter.updateData(mGoodsList, mPhotoList);
    }
}
